//
//  CHSimpleTabCell.swift
//  CHSimpleUI
//
//  Created by cyptt on 2023/3/31.
//

import UIKit
import SnapKit

public class CHSimpleListCell: UITableViewCell,UITextFieldDelegate {
    
    private var lineBgColor:UIColor = UIColor.init(red: 225/255.0, green: 225/255.0, blue: 225/255.0, alpha: 1)
    private var leftFontColor:UIColor = UIColor.init(red: 47/255.0, green: 48/255.0, blue: 53/255.0, alpha: 1)
    private var leftFont:UIFont = UIFont.systemFont(ofSize: 14)
    
    private var centerFontColor:UIColor = UIColor.init(red: 47/255.0, green: 48/255.0, blue: 53/255.0, alpha: 1)
    private var centerFont:UIFont = UIFont.systemFont(ofSize: 14)
    
    private var rightFontColor:UIColor = UIColor.init(red: 47/255.0, green: 48/255.0, blue: 53/255.0, alpha: 1)
    private var rightFont:UIFont = UIFont.systemFont(ofSize: 14)
    
    private var MARGIN_L_R:CGFloat = 20
 
    public var lineLeftCons:Constraint?  //左边 分割线
    public var lineRightCons:Constraint? //右边
    
    public var rightImageV2WCons:Constraint? //右边第二个图片宽度
    public var rightImageV2HCons:Constraint? ////右边第二个图片高度
    private var inputResultCallBack:((_ textResult:String) -> ())?
    public override func awakeFromNib() {
        super.awakeFromNib()
        self.setUI()
    }

    
   public override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.setUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
        

    }
    public func cellInputResultCallBack(clouse:@escaping(_ textResult:String)-> Void){
        self.inputResultCallBack = clouse
    }
  
    func setUI(){
        
        self.selectionStyle = .none
        self.addSubview(self.lineView)
        
        self.addSubview(self.centerL)

        
        self.addSubview(self.leftImageV)
        self.addSubview(self.leftL)

        
        self.addSubview(self.rightImageV)
        self.addSubview(self.rightL)
        self.addSubview(self.lineViewRightV)
  
        self.contentView.addSubview(self.rightTextF)
        
      
        
        //MARK: ---线条
        self.lineView.snp.makeConstraints { make in
            make.height.equalTo(0.5)
            make.bottom.equalTo(self.snp.bottom)
            self.lineLeftCons = make.left.equalTo(self.snp.left).offset(60).constraint
            self.lineRightCons = make.right.equalTo(self.snp.right).offset(0).constraint
        }
        
        self.lineViewRightV.snp.makeConstraints { make in
            make.height.equalTo(0.5)
            make.bottom.equalTo(self.snp.bottom)
            make.left.equalTo(self.lineView.snp.right)
            make.right.equalTo(self)
        }
        
        
        //MARK: ---中间
        self.centerL.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        
        
        //MARK: ---左边
        self.leftL.snp.makeConstraints { make in
            make.left.equalTo(self.leftImageV.snp.right).offset(8)
            make.centerY.equalToSuperview()
        }
        
        self.leftImageV.snp.makeConstraints { make in
            make.left.equalTo(self.lineView.snp.left)
            make.centerY.equalToSuperview()
        }
        
        
        //MARK: - 右边
        
        self.rightImageV.snp.makeConstraints { make in
            make.right.equalTo(self.lineView.snp.right)
            make.centerY.equalToSuperview()
        }
        
        self.rightL.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(self.rightImageV.snp.left).offset(-5)
        }
        
        
        //右边输入框
        self.rightTextF.snp.makeConstraints { make in
            make.right.equalTo(self.lineView.snp.right)
            make.centerY.equalToSuperview()
            make.width.equalTo(200)
            make.height.equalTo(40)
        }
    }
   public override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    
    public lazy var lineView = {() ->UIView in
      
        let view:UIView = UIView.init()
        view.backgroundColor = lineBgColor
        return view
    }()
    
    public lazy var lineViewRightV = {() ->UIView in
      
        let view:UIView = UIView.init()
        view.backgroundColor = lineBgColor
        view.isHidden = true
        return view
    }()
    
    public lazy var rightImageV = {() -> UIImageView in
         let imageV:UIImageView = UIImageView.init()
         imageV.image = UIImage.init(named: "arrow_gray_right_chsimple", in: CHBundle.bundle(), compatibleWith: nil)
         
         return imageV;
     }()
     
    public lazy var leftImageV: UIImageView = {() -> UIImageView in
         let imageV:UIImageView = UIImageView.init()
         imageV.image = UIImage.init(named: "check_box_selected", in: CHBundle.bundle(), compatibleWith: nil)
         
         return imageV
     }()
     
     public lazy var leftL: UILabel = { ()-> UILabel in
         let lab:UILabel = UILabel.init()
         lab.text = "左边"
         lab.textColor = leftFontColor
         lab.font = leftFont
         return lab
     }()
     
    public lazy var centerL: UILabel = { ()-> UILabel in
         let lab:UILabel = UILabel.init()
         lab.text = "中间"
         lab.textColor = centerFontColor
         lab.font = centerFont
         return lab
     }()
     
    public lazy var rightL: UILabel = { ()-> UILabel in
         let lab:UILabel = UILabel.init()
         lab.text = "右边"
         lab.textColor = rightFontColor
         lab.font = rightFont
         return lab
     }()
    
    
    public  lazy var rightImageView2: UIImageView = {() -> UIImageView in
         let imageV:UIImageView = UIImageView.init()
         imageV.image = UIImage.init(named: "arrow_gray_right_chsimple", in: CHBundle.bundle(), compatibleWith: nil)
         
         return imageV
     }()

    
    
    public lazy var rightTextF: UITextField = { ()-> UITextField in
         let textF:UITextField = UITextField.init()
        textF.placeholder = "请输入"
        textF.textAlignment = .right
        textF.isHidden = true
        textF.textColor = rightFontColor
        textF.font = rightFont
        textF.delegate = self
        textF.returnKeyType = .done
         return textF
     }()
    
    
}

extension CHSimpleListCell{
    
    public func textFieldDidEndEditing(_ textField: UITextField) {
      
    
        if self.inputResultCallBack != nil {
            self.inputResultCallBack!(textField.text ?? "")
        }
        
    }
    
  
    public func textFieldDidBeginEditing(_ textField: UITextField) {
        print(textField.text ?? "")
    }
    
    public func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        self.endEditing(true)
        return true
    }
   
}
